package cn.iocoder.yudao.module.system.service.bookcollection;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.bookcollection.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.bookcollection.BookcollectionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.bookcollection.BookcollectionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 绘本集 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BookcollectionServiceImpl implements BookcollectionService {

    @Resource
    private BookcollectionMapper bookcollectionMapper;

    @Override
    public Long createBookcollection(BookcollectionSaveReqVO createReqVO) {
        // 插入
        BookcollectionDO bookcollection = BeanUtils.toBean(createReqVO, BookcollectionDO.class);
        bookcollectionMapper.insert(bookcollection);
        // 返回
        return bookcollection.getId();
    }

    @Override
    public void updateBookcollection(BookcollectionSaveReqVO updateReqVO) {
        // 校验存在
        validateBookcollectionExists(updateReqVO.getId());
        // 更新
        BookcollectionDO updateObj = BeanUtils.toBean(updateReqVO, BookcollectionDO.class);
        bookcollectionMapper.updateById(updateObj);
    }

    @Override
    public void deleteBookcollection(Long id) {
        // 校验存在
        validateBookcollectionExists(id);
        // 删除
        bookcollectionMapper.deleteById(id);
    }

    private void validateBookcollectionExists(Long id) {
        if (bookcollectionMapper.selectById(id) == null) {
            throw exception(BOOKCOLLECTION_NOT_EXISTS);
        }
    }

    @Override
    public BookcollectionDO getBookcollection(Long id) {
        return bookcollectionMapper.selectById(id);
    }

    @Override
    public PageResult<BookcollectionDO> getBookcollectionPage(BookcollectionPageReqVO pageReqVO) {
        return bookcollectionMapper.selectPage(pageReqVO);
    }

}